home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / swagg-m / hardware.swg / 0009_SECTORIO.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  8KB  |  245 lines

  1. {... so there I was, sitting in a bar when a known C Programmer  }
  2. {comes up to me and sniggers "still doing it in Pascal eh?"      }
  3. {"Yup" I replied, and tossed the bartender another hundred.      }
  4. {"Yeah well, when you're ready For a Real language, only C has   }
  5. {all the aces."                                                  }
  6. {I'm a Pascal Programmer.  I don't have to take that.  "Such as?"}
  7. {I hoped he'd bite and he did.                                   }
  8. {"Such as disk sector reading and writing For starters."         }
  9. {"Well I hope you're not bluffin', 'cause here's a trick that    }
  10. {I'll bet you ain't covered."                                    }
  11. {I pulled it out With a swish and laid it on the table.  "Even   }
  12. {provides support For >32M volumes, which the C run-time library }
  13. {manual Forgets to tell you it won't do."                        }
  14. {"Huh?  Where?"                                                  }
  15. {"Right here" I said.  "Just where it says...                    }
  16.  
  17. Program AbsReadTest;
  18.  
  19. {This Program demonstrates a C-style absread and absWrite For TP.}
  20. {As is, it reads the boot sector off drive A:, and optionally    }
  21. {Writes it out to the very last sector on drive A: (assumes 1.2Meg}
  22. {This Program IS dangerous, and is released to the public domain.}
  23. {I take no responsibility For use or misuse, deliberate or       }
  24. {accidental, of this Program or any Program which Uses the       }
  25. {techniques described herein.                                    }
  26.  
  27. {Author: Mitch Davis 3:634/384.6 +61-3-890-2062 v1.0 28-Jun-92.  }
  28.  
  29. Var bp:Pointer; {Will point to the buffer For the sector data}
  30.  
  31. Function absread (drive:Char; nsects:Word; lsect:Word; buffer:Pointer):Boolean;
  32.  
  33. {Works just like the C runtime one- including being restricted to 32M volumes!}
  34.  
  35. {drive is a Character, nsects is the number of sectors, and lsect is the first}
  36. {sector.  buffer points to the buffer you'd like filled from disk.  Function  }
  37. {returns True if there was an error, or False if all went well.               }
  38.  
  39. Var kludgebuff:Array [0..$1f] of Byte; {Read Ralf Brown's interrupt listing}
  40.     kludgePtr:Pointer;                 {Int 25h - ES:[BP+1E] may change    }
  41.  
  42. begin
  43.   kludgePtr := @kludgebuff;
  44.   absread := True;
  45.   if drive < 'A' then Exit;
  46.   if drive > 'Z' then Exit;
  47.   Asm
  48.     push  es
  49.     push  bp
  50.     push  di
  51.     les   di, kludgePtr
  52.     mov   al, drive      { Gets the passed parameter. }
  53.     and   al, 1fh        { Cvt from ASCII to drive num }
  54.     dec   al             { Adjust because A: is drive 0 }
  55.     mov   cx, nsects     { number of sectors to read }
  56.     mov   dx, lsect      { starting at sector.. }
  57.     push  ds
  58.     lds   bx, buffer      { Get the address of the buffer }
  59.     mov   bp, di
  60.     push  si
  61.     int   25h            { Do the drive read. }
  62.     pop   si             { Remove the flags int 25h leaves on stack}
  63.     pop   si
  64.     pop   ds
  65.     pop   di
  66.     pop   bp
  67.     pop   es
  68.     jc    @1
  69.     mov   ah, 0          { No errors, so set Function to False }
  70.     @1:
  71.     mov   @result, ah
  72.   end;
  73. end;
  74.  
  75. Function absWrite
  76.             (drive:Char; nsects:Word; lsect:Word; buffer:Pointer):Boolean;
  77.  
  78. {Works just like the C one - including being restricted to 32M volumes!}
  79.  
  80. {drive is a Character, nsects is the number of sectors, and lsect is the first}
  81. {sector.  buffer points to the buffer you'd like filled from disk.  Function  }
  82. {returns True if there was an error, or False if all went well.               }
  83.  
  84. Var kludgebuff:Array [0..$1f] of Byte;
  85.     kludgePtr:Pointer;
  86.  
  87. begin
  88.   kludgePtr := @kludgebuff;
  89.   absWrite := True;
  90.   if drive < 'A' then Exit;
  91.   if drive > 'Z' then Exit;
  92.   Asm
  93.     push  es
  94.     push  bp
  95.     push  di
  96.     les   di, kludgePtr
  97.     mov   al, drive      { Gets the passed parameter. }
  98.     and   al, 1fh        { Cvt from ASCII to drive num }
  99.     dec   al             { Adjust because A: is drive 0 }
  100.     mov   cx, nsects     { number of sectors to Write }
  101.     mov   dx, lsect      { starting at sector.. }
  102.     push  ds
  103.     lds   bx, buffer      { Get the address of the buffer }
  104.     mov   bp, di
  105.     push  si
  106.     int   26h            { Do the drive Write. }
  107.     pop   si             { Remove the flags int 26h leaves on stack}
  108.     pop   si
  109.     pop   ds
  110.     pop   di
  111.     pop   bp
  112.     pop   es
  113.     jc    @1
  114.     mov   ah, 0
  115.     @1:
  116.     mov   @result, ah
  117.   end;
  118. end;
  119.  
  120. Function absLread (drive:Char; nsects:Word; lsect:LongInt;
  121. buffer:Pointer):Boolean;
  122.  
  123. {This Function reads sectors on disks which have the >32M style made popular}
  124. {by Compaq Dos 3.31, MS-Dos 4+ and DR-Dos 5+.                               }
  125.  
  126. Var packet:Array [0..9] of Byte; {disk request packet - see Ralf Brown's ints}
  127.  
  128. begin
  129.   absLread := True;
  130.   if drive < 'A' then Exit;
  131.   if drive > 'Z' then Exit;
  132.   Asm
  133.     mov   ax, Word ptr lsect     {Get the LSB of the start sector}
  134.     mov   Word ptr packet[0], ax {Store it in the packet         }
  135.     mov   ax, Word ptr lsect + 2 {Get the MSB of the start sector}
  136.     mov   Word ptr packet[2], ax {Store this one too.            }
  137.     mov   ax, nsects             {How many sectors to read       }
  138.     mov   Word ptr packet[4], ax
  139.     {Insert the Pointer to the data buffer into the packet}
  140.     push  bp ; push ds
  141.     lds   dx, buffer      { Get the address of the buffer }
  142.     mov   Word ptr packet[6], dx
  143.     mov   dx, ds
  144.     mov   Word ptr packet[8], dx
  145.     mov   al, drive      { Gets the passed parameter. }
  146.     and   al, 1fh        { Cvt from ASCII to drive num }
  147.     dec   al             { Adjust because A: is drive 0 }
  148.     int   25h            { Do the drive read. }
  149.     pop   si             { Remove the flags int 25h leaves on stack}
  150.     pop   ds
  151.     pop   bp
  152.     jc    @1
  153.     mov   ah, 0
  154.     @1:
  155.     mov   @result, ah
  156.   end;
  157. end;
  158.  
  159. Function absLWrite (drive:Char; nsects:Word; lsect:LongInt;
  160. buffer:Pointer):Boolean;
  161.  
  162. {This Function Writes sectors on disks which have the >32M style made popular}
  163. {by Compaq Dos 3.31, MS-Dos 4+ and DR-Dos 5+.                                }
  164.  
  165. Var packet:Array [0..9] of Byte;
  166.  
  167. begin
  168.   absLWrite := True;
  169.   if drive < 'A' then Exit;
  170.   if drive > 'Z' then Exit;
  171.   Asm
  172.     mov   ax, Word ptr lsect
  173.     mov   Word ptr packet[0], ax
  174.     mov   ax, Word ptr lsect + 2
  175.     mov   Word ptr packet[2], ax
  176.     mov   ax, nsects
  177.     mov   Word ptr packet[4], ax
  178.     push  bp ; push ds
  179.     lds   dx, buffer
  180.     mov   Word ptr packet[6], dx
  181.     mov   dx, ds
  182.     mov   Word ptr packet[8], dx
  183.     mov   al, drive      { Gets the passed parameter. }
  184.     and   al, 1fh        { Cvt from ASCII to drive num }
  185.     dec   al             { Adjust because A: is drive 0 }
  186.     int   26h            { Do the drive Write. }
  187.     pop   si             { Remove the flags int 26h leaves on stack}
  188.     pop   ds
  189.     pop   bp
  190.     jc    @1
  191.     mov   ah, 0
  192.     @1:
  193.     mov   @result, ah
  194.   end;
  195. end;
  196.  
  197. Function LongNeeded (drive:Char):Boolean;
  198.  
  199. {This Function returns True or False depending on whether the long versions}
  200. {of absread/absWrite needed to be invoked; that is, it's a drive Formatted }
  201. {in the Dos 4+ >32M style.                                                 }
  202. {I strongly suggest you see Ralf Brown's interrupt listing For int21h subfs}
  203. {440d and 53 - they'll tell you all you know to understand the guts of this}
  204. {Function.                                                                 }
  205.  
  206. Label Escape;
  207.  
  208. Var drivestats:Array [0..31] of Byte;
  209.  
  210. begin
  211.   LongNeeded := False;
  212.   if drive < 'A' then Exit;
  213.   if drive > 'Z' then Exit;
  214.   Asm
  215.     push ds
  216.     mov  dx, ss
  217.     mov  ds, dx
  218.     lea  dx, drivestats
  219.     mov  bl, drive      { Gets the passed parameter. }
  220.     and  bl, 1fh        { Cvt from ASCII to drive num }
  221.     mov  ax, 440Dh
  222.     mov  cx, 0860h
  223.     int  21h
  224.     jc   Escape
  225.     mov  ax, Word ptr drivestats[0Fh]
  226.     or   ax, ax
  227.     jnz Escape
  228.     mov  @Result, 1
  229.   Escape:
  230.     pop  ds
  231.   end;
  232. end;
  233.  
  234. begin
  235.   getmem (bp,2048);
  236.   Writeln (LongNeeded ('A'));
  237.   Writeln (LongNeeded ('C'));
  238.   Writeln (absread  ('A',1,0,bp));
  239. (*  Writeln (absWrite ('A',1,2399,bp)); *) {remove the comments at your own}
  240.                                            {risk!!!}
  241.   freemem (bp,2048);
  242. end.
  243.  
  244. {So I bought him a drink.  The poor guy looked like he needed one....}
  245.